<?php

/**
 * This is the model class for table "vfood_user".
 *
 * The followings are the available columns in table 'vfood_user':
 * @property string $userId
 * @property string $username
 * @property string $password
 * @property string $email
 * @property integer $sex
 * @property string $dateOfBirth
 * @property string $address
 * @property string $createDate
 * @property string $lastUpdateDate
 * @property string $imageUrl
 */
class User extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return User the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'vfood_user';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('username, password, email, sex, dateOfBirth, address', 'required'),
            array('sex', 'numerical', 'integerOnly' => true),
            array('username, password, email, address, imageUrl', 'length', 'max' => 255),
            array('createDate, lastUpdateDate', 'safe'),
            array('email', 'email'),
            array('dateOfBirth', 'date', 'format'=>'yyyy-M-d'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('userId, username, password, email, sex, dateOfBirth, address, createDate, lastUpdateDate, imageUrl', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'userId' => 'User',
            'username' => 'Username',
            'password' => 'Password',
            'email' => 'Email',
            'sex' => 'Sex',
            'dateOfBirth' => 'Date Of Birth',
            'address' => 'Address',
            'createDate' => 'Create Date',
            'lastUpdateDate' => 'Last Update Date',
            'imageUrl' => 'Image Url',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('userId', $this->userId, true);
        $criteria->compare('username', $this->username, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('sex', $this->sex);
        $criteria->compare('dateOfBirth', $this->dateOfBirth, true);
        $criteria->compare('address', $this->address, true);
        $criteria->compare('createDate', $this->createDate, true);
        $criteria->compare('lastUpdateDate', $this->lastUpdateDate, true);
        $criteria->compare('imageUrl', $this->imageUrl, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    /**
     * perform one-way encryption on the password before we store it in
      the database
     */
    protected function afterValidate() {
        parent::afterValidate();
        $this->password = $this->encrypt($this->password);
    }

    public function encrypt($value) {
        return md5($value);
    }

    public function getSexText() {
        if ($this->sex == 0)
            return 'Male';
        elseif ($this->sex == 1)
            return 'Female';
        else
            return 'Not defined value : ' . $this->sex;
    }

}